package com.citcc.api.controller;

import com.citcc.business.bean.vo.SupportFileUploadVO;
import com.citcc.common.msg.Response;
import com.citcc.common.utils.ResponseUtil;
import com.citcc.common.utils.file.FileUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by czh on 2019/5/22 0022 20:04
 */
@Api(value = "基础服务", tags = "基础服务")
@RestController
@RequestMapping("/support")
public class SupportController {

    @Value("${citccIot.profile}")
    private String profile;

    @Value("${citccIot.picUrl}")
    private String picUrl;

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传文件", required = true)
    })
    @PostMapping("fileUpload")
    @ApiOperation(value = "文件上传")
    public Response<SupportFileUploadVO> fileUpload(MultipartFile file) {
        try {
            SupportFileUploadVO fileUploadVO = new SupportFileUploadVO();
            fileUploadVO.setPath(picUrl + "h5/imgFile/" + FileUploadUtils.upload(profile + "imgFile/", file));
            return ResponseUtil.success(fileUploadVO);
        } catch (IOException e) {
            return ResponseUtil.fail(null);
        }
    }
}